package com.pda.yuanjing.pages.config;

import android.content.Context;
import android.util.Log;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.BaseBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.ApiYJ;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class ConfigModelImpl extends BaseModel implements ConfigModel {
    private String tag = "ConfigModelImpl";
    private BaseBeanOut baseBeanOut = null;

    public ConfigModelImpl(Context context) {
        super(context);
    }

    @Override
    public void config(String url,  String cid, final CallBack callBack) {
        try {
            api = new Retrofit.Builder()
                    .baseUrl("http://" + url + "/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build().create(ApiYJ.class);
            BaseBeanIn baseBeanIn = new BaseBeanIn();
            baseBeanIn.setAction("CheckNet");
            baseBeanIn.setCid(cid);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(baseBeanIn));
            api.config(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            SharedPreferencesManger.setHostName(url);
                            SharedPreferencesManger.setCid(cid);
                            callBack.ontip("数据连接成功,设置完成");
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            callBack.ontip(SetConfig.connectError);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BaseBeanOut.class);
                            if (baseBeanOut != null && baseBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
}

